<?php
namespace Home\Controller;
use Think\Controller;
class MenuController extends CommonController{
	public function index(){
		$this->redirect("lists");
	}
	
	public function lists(){
		$model = M();
		$sql = "SELECT * FROM oa_sys_menu order by menu_id desc";
		$data = $model->query($sql);
		$this->assign("data",$data);
		$this->display();
	}
	
	public function add(){
		$model = M("sys_menu");
		$data = $model->where("status=1 and leve=0")->select();
		$this->assign("data",$data);
		$this->display();
	}
	
	public function insert(){
		if(!IS_POST) {$this->error("非法表单");}
//		$data = $_POST;
		$data =  M()->query("show create table oa_sys_menu");
		$model = M("sys_menu");
		$data = array(
			'menu_name' => $_POST['menu_name'],
			'action' => $_POST['action'],
			'leve' => $_POST['leve'],
			'pid' => $_POST['pid'],
			'status' => $_POST['status'],
			'type' => $_POST['type'],
			'sort' => $_POST['sort'],
		);
		$success = $model->data($data)->add();
		if($success){
			$this->systemlog("add", "菜单管理", "添加菜单---".$_POST['menu_name']);
			$this->success("添加菜单成功！","index");
		}else{
			$this->error("添加菜单失败！");
		}
	}
	
	public function edit(){
		$menu_id = I('get.menu_id');
		if(!is_id($menu_id)){ $this->error("非法参数！"); }
		$model = M("sys_menu");
		$data = $model->where("menu_id=%d",$menu_id)->find();
		$data_menu = $model->where("status=1 and leve=0")->select();
		$this->assign("data", $data);
		$this->assign("data_menu",$data_menu);
		$this->display();
	}
	
	public function save(){
		if(!IS_POST) {$this->error("非法表单");}
//		$data = $_POST;
		$model = M("sys_menu");
		$menu_id = I('post.menu_id');
		if(!is_id($menu_id)){ $this->error("非法参数！"); }
		$data = array(
			'menu_name' => $_POST['menu_name'],
			'action' => $_POST['action'],
			'leve' => $_POST['leve'],
			'pid' => $_POST['pid'],
			'status' => $_POST['status'],
			'type' => $_POST['type'],
			'sort' => $_POST['sort'],
		);
		$success = $model->where("menu_id=%d",$menu_id)->data($data)->save();
		if($success){
			$this->systemlog("edit", "菜单管理", "修改菜单---".$_POST['menu_name']);
			$this->success("修改菜单成功！","index");
		}else{
			$this->error("修改菜单失败！");
		}
	}
	
	public function delete(){
		$menu_id = I('get.menu_id');
		if(!is_id($menu_id)){ $this->error("非法参数！"); }
		$model = M("sys_menu");
		$menu_name = $model->where("menu_id=%d",$menu_id)->getField("menu_name");
		$success = $model->where("menu_id=%d",$menu_id)->delete();
		if($success){
			$this->systemlog("delete", "菜单管理", "删除菜单---".$menu_name);
		}
	}
	
	public function select_pid(){
		$leve = I('get.leve');
		if(!is_id($leve)){ $this->error("非法参数！"); }
		$model = M("sys_menu");
		$data = $model->where("leve=%d and status=1", $leve)->order("sort")->select();
		$data_json = json_encode($data);
		$this->ajaxReturn($data_json);
	}
	
	public function status_disable(){
		$menu_id = I('get.menu_id');
		$val = I('get.val');
		if(!is_id($menu_id)){ $this->error("非法参数！"); }
		if($val != '1' && $val != '0'){ $this->error("非法参数！"); }
		$model = M("sys_menu");
		$menu_name = $model->where("menu_id=%d",$menu_id)->getField("menu_name");
		$success = $model->where("menu_id=%d",$menu_id)->setField("status",$val);
		if($success){
			if($val == "1"){
				$this->systemlog("edit", "菜单管理", "启用菜单---".$menu_name);
			}elseif($val == "0"){
				$this->systemlog("edit", "菜单管理", "禁用菜单---".$menu_name);
			}
		}
	}
	
	Public function type_disable(){
		$menu_id = I('get.menu_id');
		$val = I('get.val');
		if(!is_id($menu_id)){ $this->error("非法参数！"); }
		if($val != '1' && $val != '0'){ $this->error("非法参数！"); }
		$model = M("sys_menu");
		$menu_name = $model->where("menu_id=%d",$menu_id)->getField("menu_name");
		$success = $model->where("menu_id=%d",$menu_id)->setField("type",$val);
		if($success){
			if($val == "1"){
				$this->systemlog("edit", "菜单管理", "启用菜单连接---".$menu_name);
			}elseif($val == "0"){
				$this->systemlog("edit", "菜单管理", "禁用菜单连接---".$menu_name);
			}
		}
	}

    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>